package com.wswy.wzcx.model;

import android.text.TextUtils;
import com.google.gson.annotations.SerializedName;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONObject;

public class AdModel {

  /**
   * 自定义广告入口
   */
  public static final String SELF_AD = "zdy";
  /**
   * 头条 穿山甲广告
   */
  public static final String TTAD = "csj";
  /**
   * 腾讯 广点通广告
   */
  public static final String GDT = "gdt";


//
  //@SerializedName("startTime")
  //public long startTime;

  //@SerializedName("endTime")
  //public long endTime;

  @SerializedName("imgUrl")
  public String imgUrl;

  @SerializedName("linkUrl")
  public String linkUrl;

  @SerializedName("linkTarget")
  public int linkTarget;

  @SerializedName(value = "type",alternate = {"source"})
  public String type;

  @SerializedName("title")
  public String title;

  @SerializedName("ext")
  public String ext;

  private Map<String,String> extMap;

  public boolean showSelfAd(){
    return TextUtils.equals(SELF_AD,type);
  }


  @Override
  public String toString() {
    return "AdModel{" +
        "imgUrl='" + imgUrl + '\'' +
        ", linkUrl='" + linkUrl + '\'' +
        ", linkTarget=" + linkTarget +
        ", type='" + type + '\'' +
        '}';
  }


  public String getExtValue(String key){
    if (extMap == null){
      extMap = new HashMap<>(3);

      if (!TextUtils.isEmpty(ext)){
        try {
          JSONObject jsonObject = new JSONObject(ext);
          Iterator<String> keys = jsonObject.keys();
          while (keys.hasNext()){
            String next = keys.next();
            String value = jsonObject.optString(next);
            if (!TextUtils.isEmpty(value)){
              extMap.put(next,value);
            }
          }
        }catch (Exception e){

        }
      }

    }
    return extMap.get(key);
  }

}
